<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>品牌管理</title>
    <link rel="stylesheet" href="../css/elementui.css">
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    </style>
</head>

<body>
<div id="app">
    <!--弹窗-->
    <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="35%"
            :before-close="fetchData()">
        <el-form label-width="100px">
            <el-form-item label="品牌名称">
                <el-input  v-model="pojo.name" placeholder="品牌名称"></el-input>
            </el-form-item>
            <el-form-item label="品牌首字母">
                <el-input v-model="pojo.letter"  placeholder="品牌首字母"></el-input>
            </el-form-item>
            <el-form-item  label="图片">
                <!--<el-input  v-model="pojo.image" placeholder="图片"></el-input>-->
                <el-upload
                        class="avatar-uploader"
                        action="/upload/oss.do?folder=brand"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar" >
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="排序">
                <el-input  v-model="pojo.seq" placeholder="排序"></el-input>
            </el-form-item>
        </el-form>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="add()">提交</el-button>
    </el-dialog>
    <!--条件查询-->
    <el-form :inline="true">
        <el-form-item label="品牌名称">
            <el-input  v-model="searchMap.name" placeholder="品牌名称"></el-input>
        </el-form-item>
        <el-form-item label="品牌首字母">
            <el-input v-model="searchMap.letter"  placeholder="品牌首字母"></el-input>
        </el-form-item>
        <el-button type="primary" @click="fetchData">查询</el-button>
        <el-button type="primary" @click="pojo={},dialogVisible = true">新增</el-button>
    </el-form>
    <!--数据-->
    <el-table
            :data="tableData"
            border
            style="width: 100%">
        <el-table-column
                prop="id"
                label="ID">
        </el-table-column>
        <el-table-column
                prop="name"
                label="姓名"
                width="180">
        </el-table-column>
        <el-table-column
                prop="letter"
                label="首字母">
        </el-table-column>
        <el-table-column
                label="图片"
                width="180">
            <template slot-scope="scope">
                <img width="60px" height="60px" :src="scope.row.image">
            </template>
        </el-table-column>
        <el-table-column
                label="操作">
            <template slot-scope="scope">
                <el-button type="text" @click="edit(scope.row)">修改</el-button>
                <el-button type="text" @click="dele(scope.row.id)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <!--分页条-->
    <el-pagination
            @size-change="fetchData"
            @current-change="fetchData"
            :current-page.sync="currentPage"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>
</div>
</body>
<script src="../js/vue.js"></script>
<script src="../js/elementui.js"></script>
<script src="../js/axios.js"></script>


<script>
    new Vue({
        el:"#app",
        data(){
            return {
                tableData:[],
                currentPage:1,
                pageSize:10,
                total:10,
                searchMap:{},
                dialogVisible:false,
                pojo:{},
                imageUrl:''
            }
        },
        created() {
            /*axios.get("/brand/findAll.do").then(response=> {
                this.tableData = response.data;
            })*/
            this.fetchData();
        },
        methods:{
           /* fetchData(){
                axios.get(`/brand/findByPage.do?page=${this.currentPage}&size=${this.pageSize}`).then(response=>{
                    this.tableData = response.data.rows;
                    this.total = response.data.total;
                })
            },*/
            fetchData(){
                axios.post(`/brand/findPage.do?page=${this.currentPage}&size=${this.pageSize}`,this.searchMap).then(response=>{
                    this.tableData = response.data.rows;
                    this.total = response.data.total;
                })
            },
            edit(pojo){
                this.dialogVisible = true;
                this.pojo = pojo;
                this.imageUrl = this.pojo.image;
            },
            add(){
                this.pojo.image = this.imageUrl;
                axios.post(`/brand/${this.pojo.id==null?'add':'update'}.do`,this.pojo).then(response=>{
                    this.fetchData();
                    this.imageUrl='';
                    this.dialogVisible = false;

                })
            },
            dele(id){
                    this.$confirm("确认要删除吗？","提示",{
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(()=>{
                        axios.get(`/brand/delete.do?id=${id}`).then(response=>{
                            if(response.data.code == 0){
                                this.fetchData();
                            }else {
                                this.$alert(response.data.message)
                            }
                        })
                    })
            },
            handleAvatarSuccess(res, file) {
                this.imageUrl = file.response;
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
        }
    });
</script>
</html>